package com.elite.groovy.callback
import groovy.json.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import com.elite.groovy.util.EliteBean;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.wisdge.dataservice.Result;
import groovy.swing.impl.DefaultAction;
import java.util.Map;
import java.util.Date;
import java.util.HashMap;
import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;
//import net.sf.json.JSONArray;
import com.elite.groovy.util.Util;
import cn.com.kh.HttpClientProxy;
import java.awt.print.Book;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class DeliveryInquiryService implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(DeliveryInquiryService.class);
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {
		response.setCharacterEncoding("UTF-8");
		log.debug("start ----------DeliveryInquiryService-----------------");
		String getMethod = request.getMethod();
		String inData = "";
		String outData;
		String outData1 = "";
		String outData2 = "";
		String reData = "";
		String reData1 = "";
		String I_sdate = "";
		String dataSourceID = "dataSource";
		String Request_Type="";//返回类型
		String Response_Code="";//返回代码
		String Error_Message="";//返回描述
		BufferedReader iny = null;
		if (getMethod.equals("GET")) {
			inData = request.getParameter("data");
			log.debug("原始Get请求参数:" + inData);
		} else {
			InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
			iny = new BufferedReader(reader);
			String line;
			while ((line = iny.readLine()) != null) {
				inData += line;
			}
			log.debug("原始Post请求参数:" + inData);
		}
		try{
			JSONObject jsonObject1 = new JSONObject(inData);
			reData1 = GetJV(jsonObject1, "requestData");
			JSONObject jsonObject2 = new JSONObject(reData1);
			reData = GetJV(jsonObject2, "policyNoList");
			def I_requestType = GetJV(jsonObject1, "requestType");
			def I_requestId = GetJV(jsonObject1, "requestId");
			def I_companyId = GetJV(jsonObject1, "companyId");
			//log.debug("返回reData：" +reData);
			//log.debug("返回requestType：" +I_requestType);
			try{
				JSONArray jsonArray = new JSONArray(reData);
			    int dataArrayLen = jsonArray.length();
				JSONObject dataObject = null;
				JSONObject dataObject1 = null;
                def OUTArray="";
				for(int i = 0; i < dataArrayLen; i++) {
				dataObject = jsonArray.getJSONObject(i);                 
                def I_policyNo = GetJV(dataObject,"policyNo");
				def deleteSQL="select case when t1.insurancestatus =12 then  '待配送' when t1.insurancestatus =24 then '配送完成' else '开始配送' end insurancestatus, t2.expressid, case when t3.company is not null then  t3.company else  t2.company end company ,to_char(sysdate,'yyyymmddhh24miss')  sdate   from tabcrelation t1 left join tabcdeliveryinsur t2 on t1.delivery_relation_id = t2.relation_id left join c_Trans_Company t3 on t2.company =t3.status where t1.status = 1 and t1.policy_no='" + I_policyNo + "'  and rownum=1";
				def queryList=eliteBean.chaxun_sql(deleteSQL, "dataSource");
				//log.debug("queryList:" + queryList);
				def queryMap1=queryList[0];
				//log.debug("queryMap1:" + queryMap1);				
				if(queryMap1!=null){
				def I_deliveryschedulecode=queryMap1.get("insurancestatus");
				def I_expressno=queryMap1.get("expressid");
				def I_expresscompany=queryMap1.get("company");
				I_sdate=queryMap1.get("sdate");
				outData1 = "{\"policyNo\": \""+ I_policyNo +"\",\"deliveryScheduleCode\": \""+ I_deliveryschedulecode +"\",\"expressNo\": \""+ I_expressno +"\",\"expressCompany\": \""+ I_expresscompany +"\"},";
				}else{
				outData1="";
				}
			    outData2=outData2+outData1;
				}
				log.debug("处理失败返回outData2:" + outData2);
				if(outData2!=""){
				outData= "{\"requestType\": \""+ I_requestType +"\",\"requestId\": \""+ I_requestId +"\",\"responseTime\": \""+ I_sdate +"\",\"companyId\": \""+ I_companyId +"\",\"resultCode\": \"000000\",\"resultMsg\":\"成功\", \"responseData\":{\"deliveryInfoList\":["+outData2+"]}}";
				String Str1 = outData;
				int str2=Str1.lastIndexOf( "," );
				String str="";
			    StringBuilder str3 = new StringBuilder(Str1);
			    StringBuilder outData3 = str3.replace(str2, str2+1, str);
				outData=outData3.toString();
				}else{
				outData= "{\"requestType\": \""+ I_requestType +"\",\"requestId\": \""+ I_requestId +"\",\"responseTime\": \""+ I_sdate +"\",\"companyId\": \""+ I_companyId +"\",\"resultCode\": \"000000\",\"resultMsg\":\"成功\", \"responseData\":\"未查询到任何数据\"}";	
				}
               //log.debug("处理失败返回outData:" + outData);
			}catch(Exception e){
				outData = "{\"code\": 0,\"message\":\""+ e.getMessage() +"\"}";
				log.debug("处理失败返回JSON:" + outData);
				return outData;
			}
		def insertSQL2="insert into qt_dm_ifc_log_new(id,offer_no,ifc_no,send_info,return_info,send_time,return_time) values (Sys_Guid(),'" + I_requestId + "','P2','" + inData + "','" + outData + "',sysdate,sysdate)";
		eliteBean.update(insertSQL2,'dataSource');
		}catch(Exception e){
		  log.error("接口处理异常"+e.getMessage());
		  outData = "{\"code\": 0,\"message\":\""+ e.getMessage() +"\"}";
		  return outData;
		}
		log.debug("处理接口成功并返回JSON字符串Result_Json:" + outData);
		return outData;
    }		
	// 解析Json值
	public static String GetJV(JSONObject inJson, String getKey) {
		String Rev_Value = null;
		try {
			Rev_Value = inJson.getString(getKey);
		} catch (Exception e) {
			try {
				Rev_Value = String.valueOf(inJson.getInt(getKey));
			} catch (Exception e1) {
				try {
					Rev_Value = String.valueOf(inJson.getJSONArray(getKey));
				} catch (Exception e2) {
					try {
						Rev_Value = String.valueOf(inJson.getJSONObject(getKey));
					} catch (Exception e3) {

						try {
							Rev_Value = String.valueOf(inJson.getBoolean(getKey));
						} catch (Exception e4) {
							Rev_Value = "";
						}
					}
				}
			}
		} finally {
			if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
				Rev_Value = "";
			} else {
			}
		}
		return Rev_Value;
	}
}
